<div class="page-body">
    <div class="row">
        <div class="widget">
            <div class="widget-header
            <switch name="T_type">
            <case value="1">bg-magenta</case>
            <case value="2">bg-palegreen</case>
            <case value="3">bg-lightred</case>
            <case value="4">bg-blue</case>
            <case value="5">bg-blueberry</case>
            <case value="6">bg-gold</case>
            <default />bg-magenta
            </switch>
            ">
            <span class="widget-caption">{$T_title|default=CONTROLLER_NAME}</span>
            <!--Widget Buttons-->
        </div>
        <div class="widget-body">
            <div id="horizontal-form">
                <form class="form-horizontal form_buy" role="form"
                      action="{:U('doRent')}"
                      method="post" enctype="multipart/form-data" onSubmit="return false;">
                    <div class="form-group">
                        <label for="card_id" class="col-sm-3 control-label no-padding-right">会员卡：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" id="card_id" name="card_id" value="" onkeydown="getCard()">
                            <div id="card_info" style="display:none;">
                                <span class="info"></span><br>
                                购书折扣：<span class="discount_buy"></span><br>
                                活动报名费折扣：<span class="discount_apply"></span><br>
                                借阅可免运费次数：<span class="num_delivery"></span><br>
                                活动报名费可免次数：<span class="num_activity"></span><br>
                            </div>
                        </div>
                    </div>
                    <hr>
                    <div class="form-group   ">
                        <label class="col-sm-3 control-label no-padding-right">
                            条形码：</label>
                        <div class="col-sm-3">
                            <input type="text" id="tm" name="tm" class="form-control" value="" onkeydown="addBook()">
                        </div>
                    </div>
                    <div class="form-group   ">
                        <label class="col-sm-3 control-label no-padding-right"></label>
                        <div class="col-sm-6">
                            <p class="num_book">共0本</p>
                            <input type="button" class="btn btn-primary" value="提交" onkeypress="FSubmit()" onclick="FSubmit()">
                            <p class="label label-darkpink graded error_info"></p><br>
                            <br>
                            &nbsp;
                            <table class="table table-hover" aria-describedby="simpledatatable_info">
                                <thead class="bordered-darkorange">
                                <tr>
                                    <th>条码</th>
                                    <th>书名</th>
                                    <th>作者</th>
                                    <th>定价
                                        <!--/折扣价-->
                                    </th>
                                    <th>可借库存</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody class="order_book_list">
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <hr>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right">借书明细：</label>
                        <div class="col-sm-6">
                            <table class="table table-hover" aria-describedby="simpledatatable_info">
                                <thead class="bordered-darkorange">
                                <tr>
                                    <th>ID</th>
                                    <th>条码</th>
                                    <th>书名</th>
                                    <th>借阅</th>
                                    <th>归还/破损</th>
                                    <th>借阅时间</th>
                                    <th>状态</th>
                                </tr>
                                </thead>
                                <tbody id="record_list">

                                </tbody>
                                <tfoot id="record_page" style="display:none;">
                                <tr>
                                    <td colspan="3" class="page_info">
                                        共<span class="num">3</span>条记录,当前第<span class="page">1</span>页
                                    </td>
                                    <td colspan="3">
                                        <!--查看第-->
                                        <!--<select id="page_select" onchange="getRecord()" >-->
                                        <!--<option value="1">1</option>-->
                                        <!--</select>页-->
                                    </td>
                                </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>

                    <div class="form-group  ">
                        <div class="col-sm-offset-3 col-sm-10">
                            <!--<p class="label label-darkpink graded error_info"></p><br>-->

                            <input type="hidden" id="user_id" name="user_id" value="">
                            <!--图书列表-->
                            <input type="hidden" id="liststr" name="liststr" value="">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</div>
<script>
    var $rentmax={:C("rentmax_md")};
    var $list_name = [];
    //获取会员卡成功后回调
    function getsuccess(data) {
        $("#user_id").val(data.data.user_id);
        $("#discount_buy").val(data.data.discount_buy);
        $("#card_info").show();
        $("#tm").focus();
        getRecord();
    }
    //获取当前会员卡已借阅记录
    function getRecord() {
        var $user_id = $("#user_id").val();
        var $list = $("#record_list");
        var $page = $("#page_select").val();
        $list.html("");
        $("#record_page").hide();
        $.post("{:U('Rent/getRecord')}", {"user_id": $user_id, "page": $page}, function (data) {
            if (data.status == "success") {
                //添加借阅记录
                $.each(data.data, function (i, item) {
                    if (item.is_backed == "1") {
                        item.is_backed = "已归还";
                        var $class = "";
                    } else {
                        item.is_backed = "未归还";
                        var $class = "warning";
                    }
                    var $str = "<tr class='" + $class + "'>" +
                        "<td>" + item.order_id + "</td>" +
                        "<td>" +
                        //                            "" + item.book_id + "/" +
                        "" + item.tm + "</td>" +
                        "<td>《" + item.sm + "》</td>" +
                        "<td>" + item.quantity + "本</td>" +
                        "<td>" + item.num_backed + "本/" + item.num_broken + "本</td>" +
                        "<td>" + item.create_time + "</td>" +
                        "<td>" + item.is_backed + "</td>" +
                        "</tr>";
                    $list.append($str);
                });
                $("#page_select").html("");
                if (data.total == "0") {
                    data.total = 1;
                }
                for (var i = 1; i <= data.total; i++) {
                    if (i == data.page) {
                        $str = '<option value="' + i + '" selected="selected">' + i + '</option>';
                    } else {
                        $str = '<option value="' + i + '">' + i + '</option>';
                    }
                    $("#page_select").append($str);
                }
                $("#record_page .page_info .num").html(data.total);
                $("#record_page .page_info .page").html($("#page_select").val());
                $("#record_page").show();
            } else {
                console.debug(data.data);
            }
            $("#page_select").focus();
        }, "json");
    }
    //添加图书
    function addBook() {
        $("#tm").focus();
        var keyCode = null;

        if (event.which)
            keyCode = event.which;
        else if (event.keyCode)
            keyCode = event.keyCode;

        if (keyCode == 13) {

            var $tm = $("#tm").val();
            if ($.trim($tm) == "") {
                $("#tm").val("");
                $("#tm").focus();
                return;
            }
            $(".error_info").html('');
            var $user_id = $("#user_id").val();
            if ($user_id == "") {
                $(".error_info").html("请输入会员卡");
                return;
            }

            if(getBookListNum()>=$rentmax){
                $(".error_info").html('已超过本次借阅上限,最多借阅'+$rentmax+'本');
                return;
            }
            $.post("{:U('Book/ob_search')}", {"tm": $tm,'user_id':$user_id}, function (data) {
                if (data.status == "error") {
                    alert(data.data);
                } else {
                    //调用模态框选择数据
                    getSelectBook(data);
                }
            }, "json");
        }
    }
    //模态框选择后回调
    function doadd(item) {
        if(item.is_rented > 0){
            $('#modal-confirm1 #modal-confirm-body').text('已有借阅记录，是否再次借阅?');
            $('#modal-confirm1').modal('show');
            $("#modal-confirm1 .btn-clear").focus();
            setTimeout(function(){
                $("#modal-confirm1 .btn-clear").focus();
            },500);
            $('#modal-confirm1 .btn-ok').click(function(){
                $('#modal-confirm1 .btn-ok').unbind("click");
                $('#modal-confirm1').modal('hide');
                var $liststr=$("#liststr").val();
                var newName = $list_name.join('');
                if($liststr.indexOf(item.book_id) > -1 || newName.indexOf(item.sm) > -1){
                    $('#modal-confirm2 #modal-confirm-body').text('已加入借阅列表，是否再次加入?');
                    $('#modal-confirm2').modal('show');
                    $("#modal-confirm2 .btn-clear").focus();
                    setTimeout(function(){
                        $("#modal-confirm2 .btn-clear").focus();
                    },500);
                    $('#modal-confirm2 .btn-ok').click(function(){
                        $('#modal-confirm2 .btn-ok').unbind("click");
                        $('#modal-confirm2').modal('hide');

                        $(".order_book_list tr").removeClass("active");
                        $str = '<tr class="active">' +
                            '<td>' +
                            '<span class="book_id" style="display:none;">' + item.book_id + '</span>' +
                            //                '/' +
                            '<span class="tm">' + item.tm + '</span></td>' +
                            '<td class="sm" title="' + item.sm + '">《' + item.sm + '》</td>' +
                            '<td>' + item.zyz + '</td>' +
                            '<td>￥<span class="dj">' + item.dj + '</span>元' +
                            //折扣价
                            //                '&nbsp;&nbsp;/&nbsp;&nbsp;<br>￥<span class="dj_d">' + item.dj_d + '</span>元</td>' +
                            '<td>' + item.stock_rent + '本</td>' +
                            '<td><span class="btn btn-danger btn btn-danger action-btn" onclick="delBook($(this))">删除</span></td>' +
                            '</tr>';
                        $(".order_book_list").prepend($str);
                        $("#tm").val("");
                        $("#tm").focus();
                        cal_all();
                    });
                    $('#modal-confirm2 .btn-clear').click(function(){
                        $('#modal-confirm2 .btn-ok').unbind("click");
                    });
//            if (!confirm('已加入借阅列表，是否再次加入?')) {
//                return;
//            }
                }else{

                    $(".order_book_list tr").removeClass("active");
                    $str = '<tr class="active">' +
                        '<td>' +
                        '<span class="book_id" style="display:none;">' + item.book_id + '</span>' +
                        //                '/' +
                        '<span class="tm">' + item.tm + '</span></td>' +
                        '<td class="sm" title="' + item.sm + '">《' + item.sm + '》</td>' +
                        '<td>' + item.zyz + '</td>' +
                        '<td>￥<span class="dj">' + item.dj + '</span>元' +
                        //折扣价
                        //                '&nbsp;&nbsp;/&nbsp;&nbsp;<br>￥<span class="dj_d">' + item.dj_d + '</span>元</td>' +
                        '<td>' + item.stock_rent + '本</td>' +
                        '<td><span class="btn btn-danger btn btn-danger action-btn" onclick="delBook($(this))">删除</span></td>' +
                        '</tr>';
                    $(".order_book_list").prepend($str);
                    $("#tm").val("");
                    $("#tm").focus();
                    cal_all();

                }
            });
            $('#modal-confirm1 .btn-clear').click(function(){
                $('#modal-confirm1 .btn-ok').unbind("click");
            });
//            if (!confirm('已有借阅记录，是否再次借阅?')) {
//                return;
//            }

        }else{
            var $liststr=$("#liststr").val();
            var newName = $list_name.join('');
            if($liststr.indexOf(item.book_id) > -1 || newName.indexOf(item.sm) > -1){
                $('#modal-confirm2 #modal-confirm-body').text('已加入借阅列表，是否再次加入?');
                $('#modal-confirm2').modal('show');
                $("#modal-confirm2 .btn-clear").focus();
                setTimeout(function(){
                    $("#modal-confirm2 .btn-clear").focus();
                },500);
                $('#modal-confirm2 .btn-ok').click(function(){
                    $('#modal-confirm2 .btn-ok').unbind("click");
                    $('#modal-confirm2').modal('hide');

                    $(".order_book_list tr").removeClass("active");
                    $str = '<tr class="active">' +
                        '<td>' +
                        '<span class="book_id" style="display:none;">' + item.book_id + '</span>' +
                        //                '/' +
                        '<span class="tm">' + item.tm + '</span></td>' +
                        '<td class="sm" title="' + item.sm + '">《' + item.sm + '》</td>' +
                        '<td>' + item.zyz + '</td>' +
                        '<td>￥<span class="dj">' + item.dj + '</span>元' +
                        //折扣价
                        //                '&nbsp;&nbsp;/&nbsp;&nbsp;<br>￥<span class="dj_d">' + item.dj_d + '</span>元</td>' +
                        '<td>' + item.stock_rent + '本</td>' +
                        '<td><span class="btn btn-danger btn btn-danger action-btn" onclick="delBook($(this))">删除</span></td>' +
                        '</tr>';
                    $(".order_book_list").prepend($str);
                    $("#tm").val("");
                    $("#tm").focus();
                    cal_all();
                });
                $('#modal-confirm2 .btn-clear').click(function(){
                    $('#modal-confirm2 .btn-ok').unbind("click");
                });
//            if (!confirm('已加入借阅列表，是否再次加入?')) {
//                return;
//            }


            }else{
                $(".order_book_list tr").removeClass("active");
                $str = '<tr class="active">' +
                    '<td>' +
                    '<span class="book_id" style="display:none;">' + item.book_id + '</span>' +
                    //                '/' +
                    '<span class="tm">' + item.tm + '</span></td>' +
                    '<td class="sm" title="' + item.sm + '">《' + item.sm + '》</td>' +
                    '<td>' + item.zyz + '</td>' +
                    '<td>￥<span class="dj">' + item.dj + '</span>元' +
                    //折扣价
                    //                '&nbsp;&nbsp;/&nbsp;&nbsp;<br>￥<span class="dj_d">' + item.dj_d + '</span>元</td>' +
                    '<td>' + item.stock_rent + '本</td>' +
                    '<td><span class="btn btn-danger btn btn-danger action-btn" onclick="delBook($(this))">删除</span></td>' +
                    '</tr>';
                $(".order_book_list").prepend($str);
                $("#tm").val("");
                $("#tm").focus();
                cal_all();

            }

        }


//        曾经的代码
//        if(item.is_rented>0){
//            if (!confirm('已有借阅记录，是否再次借阅?')) {
//                return;
//            }
//        }
//        var $liststr=$("#liststr").val();
//        if($liststr.indexOf(item.book_id) > -1){
//            if (!confirm('已加入借阅列表，是否再次加入?')) {
//                return;
//            }
//        }
//        $(".order_book_list tr").removeClass("active");
//        $str = '<tr class="active">' +
//                '<td>' +
//                '<span class="book_id" style="display:none;">' + item.book_id + '</span>' +
////                '/' +
//                '<span class="tm">' + item.tm + '</span></td>' +
//                '<td class="sm" title="' + item.sm + '">《' + item.sm + '》</td>' +
//                '<td>' + item.zyz + '</td>' +
//                '<td>￥<span class="dj">' + item.dj + '</span>元' +
//                //折扣价
////                '&nbsp;&nbsp;/&nbsp;&nbsp;<br>￥<span class="dj_d">' + item.dj_d + '</span>元</td>' +
//                '<td>' + item.stock_rent + '本</td>' +
//                '<td><span class="btn btn-danger btn btn-danger action-btn" onclick="delBook($(this))">删除</span></td>' +
//                '</tr>';
//        $(".order_book_list").prepend($str);
//        $("#tm").val("");
//        $("#tm").focus();
//        cal_all();


    }
    function delBook($this) {
        if (!confirm('确认删除?')) {
            return;
        }
        $this.parent().parent().remove();
        cal_all();
    }
    //计算总金额及图书list
    function cal_all() {
        var $discount = $(".discount_buy").html();
        if ($discount == "" || $discount == null) {
            $discount = 1;
        }
        var $num_book = 0; //图书数量
        var $list = [];
        $(".order_book_list tr").each(function (i, item) {
            var $dj = item.getElementsByClassName("dj")[0].innerHTML;
            var $dj_d = parseFloat($dj * $discount).toFixed(2);
            var $book_id = item.getElementsByClassName("book_id")[0].innerHTML;
            var $book_name = item.getElementsByClassName("sm")[0].innerHTML;
            $list.push($book_id);
            $list_name.push($book_name);
            $num_book++;
        });
        $(".num_book").html("共" + $num_book + "本");
        $("#liststr").val($list);
    }
    //提交
    function FSubmit() {
        var aa1 = getCard_post();
        var aa2 = cal_all();
        var $liststr = $("#liststr").val();
        var $card_id = $("#card_id").val();
        if (!confirm('确认操作?')) {
            return;
        }
        if ($("#user_id").val() == "") {
            $(".error_info").html("请输入会员卡");
            return;
        }
        if ($liststr == "") {
            $(".error_info").html("订单图书不能为空");
            return;
        }
        $.post("{:U('Rent/rentCheck')}", {"liststr": $liststr, "card_id": $card_id}, function (data) {
            if (data.status == "success") {
                $(".form_buy").attr("onSubmit", "");
                $(".form_buy").submit();
                return true;
            } else {
                console.log(data.data);
                alert(data.data);
            }
        }, "json");
    }
    function getBookListNum(){
        return $(".order_book_list tr").length+$("#record_list .warning").length;
    }
</script>